package com.example.yichuangyun.mvvm.ui.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;

import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.FragmentDetailsFollowRecordBinding;
import com.example.yichuangyun.http.HttpRespond;
import com.example.yichuangyun.http.MyObserver;
import com.example.yichuangyun.http.RetrofitHelper;
import com.example.yichuangyun.model.EventBusModel;
import com.example.yichuangyun.model.business.FollowDataModel;
import com.example.yichuangyun.mvvm.base.BaseFragment;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.ui.adapter.FollowAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 商机详情-跟进记录
 */
public class ContactsDetailsFollowRecordFragment extends BaseFragment<FragmentDetailsFollowRecordBinding, BaseViewModel> {

    private int page = 1;
    private List<FollowDataModel.FollowRecordModel> followData = new ArrayList<>();
    private FollowAdapter followAdapter;
    private String id;

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void lazyLoadData() {
        EventBus.getDefault().register(this);
        Bundle bundle = getArguments();
        if (bundle != null) {
            id = bundle.getString(Constant.idKey);
        }
        getFollowRecord();
    }

    /**
     * 跟进记录
     */
    private void getFollowRecord() {
        JSONObject requestDate = new JSONObject();
        try {
            requestDate.put("id", id);
            requestDate.put("findType", null);
            requestDate.put("page", page);
            requestDate.put("pageSize", Constant.pageSize);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), requestDate.toString());
        RetrofitHelper.getInstance().execute(RetrofitHelper.getService().getContactsFollowLog(requestBody), new MyObserver<HttpRespond<FollowDataModel>>() {
            @Override
            protected void onSuccess(HttpRespond<FollowDataModel> followDataModelHttpRespond) {
                if (Constant.SUCCESS == followDataModelHttpRespond.code) {
                    if (followDataModelHttpRespond.data != null) {
                        List<FollowDataModel.FollowRecordModel> list = followDataModelHttpRespond.data.getRecords();
                        if (list != null && list.isEmpty()) {
                            binding.smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }
                        binding.smartRefreshLayout.setEnableLoadMore(followDataModelHttpRespond.data.getTotal() > 10);
                        if (page == 1) {
                            followData = list;
                            followAdapter.onRefresh(list);
                            binding.smartRefreshLayout.finishRefresh();
                        } else {
                            followAdapter.addData(list);
                            binding.smartRefreshLayout.finishLoadMore();
                        }
                    }
                }
            }
        });
    }

    @Override
    public void initCtrl() {
        followAdapter = new FollowAdapter(mContext, followData, 1);
        binding.recyclerView.setAdapter(followAdapter);
    }

    @Override
    protected void initClick() {
        binding.smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getFollowRecord();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getFollowRecord();
            }
        });
    }

    public static ContactsDetailsFollowRecordFragment newInstance(String id) {
        Bundle bundle = new Bundle();
        bundle.putString(Constant.idKey, id);
        ContactsDetailsFollowRecordFragment detailsFollowRecordFragment = new ContactsDetailsFollowRecordFragment();
        detailsFollowRecordFragment.setArguments(bundle);
        return detailsFollowRecordFragment;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(EventBusModel eventBusModel) {
        if (null != eventBusModel) {
            if (eventBusModel.getMsg().equals(Constant.refreshFollowList)) {
                getFollowRecord();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
